package com.jstyle.jclife.activity;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jstyle.jclife.BuildConfig;
import com.jstyle.jclife.R;
import com.jstyle.jclife.fragment.BaseFragment;
import com.jstyle.jclife.fragment.LoginFragment;
import com.jstyle.jclife.fragment.SignInFragment;
import com.jstyle.jclife.utils.PermissionsUtil;
import com.jstyle.jclife.utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginOrSingInActivity extends BaseActivity {

    @BindView(R.id.rg_login)
    RadioGroup rgLogin;
    @BindView(R.id.frameLayout_content)
    FrameLayout frameLayoutContent;
    public static final int FRAGMENT_LOGIN = 0;
    public static final int FRAGMENT_SIGN = 1;
    private static final String TAG = "LoginOrSingInActivity";
    @BindView(R.id.iv_switch_login)
    ImageView ivSwitchLogin;
    @BindView(R.id.ll_login1)
    LinearLayout llLogin1;
    @BindView(R.id.tv_other_way)
    TextView tvOtherWay;
    private FragmentManager fragmentManager;
    LoginFragment loginFragment;
    SignInFragment signInFragment;
    List<Fragment> fragmentList = new ArrayList<>();
    Map<String, Fragment> fragmentMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login_or_sing_in);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        rgLogin.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int fragment_mode = 0;
                switch (checkedId) {
                    case R.id.rb_login:
                        fragment_mode = FRAGMENT_LOGIN;
                        tvOtherWay.setText(getString(R.string.another_way_to_login));
                        break;
                    case R.id.rb_sign:
                        fragment_mode = FRAGMENT_SIGN;
                        tvOtherWay.setText(getString(R.string.another_way_to_registation));
                        break;
                }
                startTransaction(fragment_mode);
            }
        });
        if (savedInstanceState != null) {
            fragmentList = fragmentManager.getFragments();
        }
        startTransaction(FRAGMENT_LOGIN);
        requestPermissions();
       // showOtherLoginWay();
    }

    private void requestPermissions() {
        PermissionsUtil.requestPermissions(this, new PermissionsUtil.PermissionListener() {
            @Override
            public void granted(String name) {

            }

            @Override
            public void NeverAskAgain() {

            }

            @Override
            public void disallow(String name) {

            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    private void showOtherLoginWay() {
        boolean isShow = BuildConfig.APPLICATION_ID.equals("com.jstyle.jclife");
        llLogin1.setVisibility(isShow ? View.VISIBLE : View.GONE);
        ivSwitchLogin.setVisibility(isShow ? View.VISIBLE : View.GONE);
         isPhoneAccount = SharedPreferenceUtils.getBoolean(SharedPreferenceUtils.KEY_account_type,true);
        ivSwitchLogin.setImageResource(isPhoneAccount ? R.drawable.login_email : R.drawable.login_phone);

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        // super.onSaveInstanceState(outState, outPersistentState);
    }

    protected void startTransaction(int fragmentId) {
        //  Log.i(TAG, "startTransaction: "+fragmentId);
        if (fragmentId == -1) return;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        Fragment fragment = findFragment(fragmentId);
        if (fragment != null) {
            fragmentTransaction.show(findFragment(fragmentId));
        } else {
            switch (fragmentId) {
                case FRAGMENT_LOGIN:
                    fragment = new LoginFragment();
                    break;
                case FRAGMENT_SIGN:
                    fragment = new SignInFragment();
                    break;
            }
            addFragmentTag(fragmentTransaction, fragmentId, fragment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void addFragmentTag(FragmentTransaction fragmentTransaction, int tag, Fragment fragment) {
        fragmentList.add(fragment);
        fragmentTransaction.add(R.id.frameLayout_content, fragment, String.valueOf(tag));
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentList) {
            fragmentTransaction.hide(fragment);
        }
    }

    private Fragment findFragment(int tag) {
        for (Fragment fragment : fragmentList) {
            if (fragment.getTag().equals(String.valueOf(tag))) return fragment;
        }
        return null;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void finishSignIn() {
        super.finishSignIn();
        rgLogin.check(R.id.rb_login);
        Log.i(TAG, "finishSignIn: ");
    }

    boolean isPhoneAccount = true;

    @OnClick(R.id.iv_switch_login)
    public void onViewClicked() {
        isPhoneAccount = !isPhoneAccount;
        ivSwitchLogin.setImageResource(isPhoneAccount ? R.drawable.login_email : R.drawable.login_phone);
        SharedPreferenceUtils.setSpBoolean(SharedPreferenceUtils.KEY_account_type, isPhoneAccount);
        for (Fragment fragment : fragmentList) {
            if (fragment.isAdded())
                ((BaseFragment) fragment).switchAccountType(isPhoneAccount);
        }
    }
}
